package cn.ll.service;

import static org.junit.Assert.fail;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import cn.ll.po.Connector;
import cn.ll.po.ConnectorDAO;
import cn.ll.po.Customer;
import cn.ll.po.CustomerDAO;
import cn.ll.po.Department;
import cn.ll.po.Permissions;
import cn.ll.po.User;
import cn.ll.util.SystermUtil;
import cn.ll.vo.LoginUser;

public class CustomerService {

	CustomerDAO customerDAO = null;
	ConnectorDAO connectorDAO = null;

	public ConnectorDAO getConnectorDAO() {
		return connectorDAO;
	}

	public void setConnectorDAO(ConnectorDAO connectorDAO) {
		this.connectorDAO = connectorDAO;
	}

	public CustomerDAO getCustomerDAO() {
		return customerDAO;
	}

	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	public Map read(Map condition, List<Permissions> readplist) {

		String readtype = (String) condition.get("readtype");
		Map map = null;
		if (readtype != null && readtype.equals("read")) {
			List<Permissions> rp = SystermUtil.usrPermissionsList("customermgr", "read", readplist);
			if (rp != null && rp.size() > 0) {
				map = this.readItems(condition, rp);
			}
		} else if (readtype != null && readtype.equals("audit")) {
			List<Permissions> rp = SystermUtil.usrPermissionsList("customeraudit", "read", readplist);
			if (rp != null && rp.size() > 0) {
				map = this.auditItems(condition, rp);
			}
		} else {
			Customer customer = this.customerDAO.findById((String) condition.get("id"));
			List ls = new ArrayList();
			ls.add(customer);
			map = new HashMap();
			map.put("list", ls);
			map.put("totalcount", ls.size());
		}

		return map;

	}

	private Map auditItems(Map condition, List<Permissions> rp) {

		Integer startline = (Integer) condition.get("startline");
		Integer num = (Integer) condition.get("num");

		Map map = this.customerDAO.findByStatus(1, startline, num);

		return map;
	}

	private Map readItems(Map condition, List<Permissions> rp) {

		String readscope = "self";
		Map map = null;
		for (int i = 0; i < rp.size(); i++) {
			Permissions permissions = rp.get(i);
			if (permissions.getScope().equalsIgnoreCase("all")) {
				readscope = "all";
				break;
			}
		}
		// 读取所有未删除的客户信息的权限
		if (readscope.equalsIgnoreCase("all")) {
			map = this.customerDAO.findCustomerNotDel(condition);
		} else {
			String uid = (String) condition.get("uid");
			Integer startline = (Integer) condition.get("startline");
			Integer num = (Integer) condition.get("num");
			if (startline == null) {
				startline = 1;
			}
			if (num == null) {
				num = 10;
			}
			map = this.customerDAO.findSalesCus(uid, startline, num);
		}
		return map;
	}

	public Map create(Customer customer, LoginUser loginUser) {
		// customer.setStatus(0);
		customer.setUserid(loginUser.getUser().getId());
		String customerid = customer.getId();

		if (customerid == null) {
			// 第一次 插入
			customerid = this.createCustomerId();
			customer.setId(customerid);

			this.customerDAO.save(customer);
			Connector connector = new Connector();
			connector.setConnectorname(customer.getContactorname());
			connector.setCustomerid(customerid);
			connector.setId(customerid + "-001");
			connector.setConnectorid(customerid + "-001");
			// connector.setAge(0);
			connector.setEmail(customer.getContactoremail());
			connector.setMb(customer.getContactmobile());
			connector.setTel(customer.getTel());
			connector.setPossition(customer.getContactorposition());
			connector.setSex(customer.getContactgender());
			connector.setPossition(customer.getContactorposition());
			// connector.setHobe("");
			// connector.setNotes("");
			this.connectorDAO.save(connector);

		} else {
			// 否则更新
			this.customerDAO.update(customer);
		}

		Map rs = new HashMap();
		rs.put("success", true);
		return rs;
	}

	private String createCustomerId() {

		// String shortName = department.getDepShortName();

		Long count = customerDAO.count();

		DecimalFormat df = new DecimalFormat("00000000");

		String id = "C" + df.format(++count);

		return id;
	}

	public void update(Customer customer) {
		customerDAO.update(customer);
	}

	public Map delete(List<Customer> customerList) {

		for (Customer customer : customerList) {

			customer = this.customerDAO.findById(customer.getId());
			customer.setStatus(3);
			this.customerDAO.attachDirty(customer);
		}

		Map rs = new HashMap();
		rs.put("success", true);
		return rs;
	}

	public Customer findById(String id) {
		Customer customer = customerDAO.findById(id);
		return customer;
	}

	public List<Customer> findByStatus(int status) {
		List<Customer> list = customerDAO.findByStatus(status);
		return list;
	}

	public Map findByStatus(int status, int start, int limit) {
		List list = customerDAO.findByStatus(status);
		Map map = new HashMap(0);
		int size = list.size();
		int toIndex = start + limit > size ? size : start + limit;
		map.put("list", list.subList(start, toIndex));
		map.put("totalcount", size);
		return map;
	}

	/**
	 * 获取客户的id和名称的对应map
	 * 
	 * @return
	 */
	public List namelist() {
		List list = null;
		list = customerDAO.selectNameList();
		return list;
	}

	/**
	 * 对客户的名称,二级单位,电话,地址,进行模糊查询
	 * 
	 * @return
	 */
	public List<Customer> fuzzyQuery(Map map) {
		String customername = (String) map.get("customername");
		String subdepartment = (String) map.get("subdepartment");
		String tel = (String) map.get("tel");
		String address = (String) map.get("address");
		List<Customer> list = customerDAO.fuzzyQuery(customername, subdepartment, tel, address);

		return list;
	}

	// @Test
	// public void testCreateCustomerId() {
	// Department department = new Department();
	// department.setDepShortName("SHA");
	// this.createCustomerId(department);
	// }

}
